package com.css.fxfzypg.modules.baseData.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzypg.base.response.RestResponse;
import com.css.fxfzypg.modules.baseData.entity.HistoricalSeismicData;
import com.css.fxfzypg.modules.baseData.entity.HistoricalSeismicDataNew;
import com.css.fxfzypg.modules.baseData.service.HistoricalSeismicDataService;
import com.css.fxfzypg.modules.baseData.queryParams.HistoricalSeismicDataQueryParams;
import com.css.fxfzypg.modules.baseData.vo.HistoricalSeismicDataVO;
import com.css.fxfzypg.util.CreateGson;
import com.css.fxfzypg.util.FileUtil;
import com.css.fxfzypg.util.PlatformObjectUtils;
import com.css.fxfzypg.util.PlatformPageUtils;
import com.css.fxfzypg.zcpt.sys.entity.SUser;
import com.css.fxfzypg.zcpt.sys.service.SUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("historicalSeismicData")
@Slf4j
public class HistoricalSeismicDataController {
    @Resource
    HistoricalSeismicDataService historicalSeismicDataService;

    /**
     * 新增
     * @return
     */
    @PostMapping("/saveHistoricalSeismicData")
    public RestResponse saveHistoricalSeismicData (@RequestBody HistoricalSeismicData historicalSeismicData){
        RestResponse restResponse = null ;
        try{
            historicalSeismicDataService.saveHistoricalSeismicData(historicalSeismicData);
            restResponse = RestResponse.succeed("新增成功");
        }catch (Exception e){
            log.error("新增历史地震数据失败",e);
            restResponse = RestResponse.fail("新增历史地震数据失败!");
        }
        return restResponse;
    }

    /**
     * 分页
     * @param request
     * @param queryParams
     * @return
     */
    @GetMapping("/getAllHistoricalSeismicData")
    public RestResponse getAllHistoricalSeismicData(HttpServletRequest request, HistoricalSeismicDataQueryParams queryParams){

        RestResponse restResponse = null;
        try{
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String, Object> allHistoricalSeismicData = historicalSeismicDataService.getAllHistoricalSeismicData(queryParams,curPage,pageSize);
            restResponse = RestResponse.succeed(allHistoricalSeismicData);
        }catch (Exception e){
            log.error("查询历史地震数据失败!",e);
            restResponse = RestResponse.fail("查询历史地震数据失败");
        }
        return restResponse;
    }

    /**
     * 根据id查询HistoricalSeismicData数据
     * @param id
     * @return
     */
    @GetMapping("/getHistoricalSeismicData")
    public RestResponse<HistoricalSeismicData> getUser(String id) {
        RestResponse restResponse = null;
        try{
            restResponse = RestResponse.succeed(historicalSeismicDataService.getHistoricalSeismicDataById(id));
        }catch (Exception e){
            log.error("获取历史地震数据失败!",e);
            restResponse = RestResponse.fail("获取历史地震数据失败!");
        }
        return restResponse;
    }


    /**
     * 删除
     * @param ids
     * @return
     */
    @PostMapping("/delHistoricalSeismicData/{ids}")
    public RestResponse deleteElevation(@PathVariable("ids") List<String> ids) {
        RestResponse restResponse = null;
        try{
            historicalSeismicDataService.deleteHistoricalSeismicDatas(ids);
            restResponse = RestResponse.succeed("删除成功");
        }catch (Exception e){
            log.error("删除历史地震数据失败!",e);
            restResponse = RestResponse.fail("删除历史地震数据失败!");
        }
        return restResponse;
    }

    /**
     * 修改
     * @param historicalSeismicData
     * @return
     */
    @PostMapping("/updateHistoricalSeismicData")
    public RestResponse<HistoricalSeismicDataVO> updateElevation(@RequestBody HistoricalSeismicData historicalSeismicData)  {
        RestResponse restResponse = null;
        historicalSeismicDataService.updateHistoricalSeismicData(historicalSeismicData);
        try{
            restResponse = RestResponse.succeed("修改成功!");
        }catch (Exception e){
            log.error("修改失败!",e);
            restResponse = RestResponse.fail("修改失败!");
        }
        return restResponse;
    }

    /**
     * 导出
     * @param response
     */
    @PostMapping("/exportExcelHistoricalSeismicData")
    public RestResponse exportExcel(HistoricalSeismicDataQueryParams queryParams,HttpServletResponse response){
        RestResponse restResponse = null;
        try {
            historicalSeismicDataService.exportExcel(response,queryParams);
            restResponse = RestResponse.succeed("导出成功");
        } catch (Exception e) {
            String errormessage = e.getMessage();
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 导入
     * @param file
     * @return
     */
    @PostMapping("/importExcelHistoricalSeismicData")
    public RestResponse importExcel(MultipartFile file){
        RestResponse response = null;
        try {
            String info=historicalSeismicDataService.importExcel(file);
            response = RestResponse.succeed(info);
        } catch (Exception e) {
            String errormessage = "导入失败";
            log.error(errormessage, e);
            response = RestResponse.fail(errormessage);
        }
        return response;
    }

    /**
     * 判断登录用户是否是国家管理员
     * @return
     */
    @GetMapping("/isCouAdmin")
    public RestResponse isCouAdmin(){
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            boolean flag=historicalSeismicDataService.isCouAdmin();
            if (flag) {
                jsonObject.put("message", "是");
            } else {
                jsonObject.put("message", "否");
            }
            restResponse = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errormessage = e.getMessage();
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 判断当前任务区域中存在那些历史地震数据
     * @return
     */
    @GetMapping("/judgeHistoricalSeismicData")
    public RestResponse judgeHistoricalSeismicData(String taskId){
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            List<HistoricalSeismicData> data=historicalSeismicDataService.judgeHistoricalSeismicData(taskId);
            restResponse = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errormessage = e.getMessage();
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }
}
